package com.yllt4cloud.common.ddbase.message;

import com.alibaba.fastjson.JSONObject;
import com.yllt4cloud.common.ddbase.constant.ErrorCode;
import com.yllt4cloud.common.ddbase.constant.entity.CryptEnum;
import com.yllt4cloud.common.ddbase.util.Base64Utils;
import com.yllt4cloud.common.ddbase.util.crypto.AESCrypto;

import java.io.Serializable;
import java.io.UnsupportedEncodingException;


/**
 * Created by Administrator on 2015/11/4.
 */
public class Message<T> implements Serializable {

    private static final long serialVersionUID = 7590999461767050471L;
    
    private String errorMsg = "运行正确";
    private int errorCode = ErrorCode.SUCCESS;
    private T result;
    private int encryptType;

    public int getEncryptType() {
        return encryptType;
    }

    public void setEncryptType(int encryptType) {
        this.encryptType = encryptType;
    }

    public String getErrorMsg() {
        return errorMsg;
    }

    public void setErrorMsg(String errorMsg) {
        this.errorMsg = errorMsg;
    }

    public int getErrorCode() {
        return errorCode;
    }

    public void setErrorCode(int errorCode) {
        this.errorCode = errorCode;
    }

    public T getResult() {
        return result;
    }

    public void setResult(T result) {
        this.result = result;
    }

    public Message(String errorMsg, int errorCode) {
        if(null!=errorMsg){
            this.errorMsg = errorMsg;
        }else {
            this.errorMsg="未知错误";
        }
        this.errorCode = errorCode;
    }

    public Message(T result) {
        this.result = result;
    }
    public Message() {
    	super();
    }

    public static  < T> Message< T > success()
    {
        return success( null );
    }

    public static < T > Message< T > success( T result )
    {
        return new Message< T >(result);
    }
    
    /**
     * 使用base64进行加密数据
     * @param result	返回的结果数据集
     * @return
     */
    public static < T > Message< T > successBase64( T result )
    {
    	
    	Message<String> Message = new Message<String>(null);
    	
    	Message.setEncryptType(Integer.parseInt(CryptEnum.BASE64.toString()));
    	
    	try {
			Message.setResult(Base64Utils.encode(JSONObject.toJSONString(result).getBytes("utf-8")));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
        return (Message<T>) Message;
    }
    
    /**
     * 使用AES进行数据加密
     * @param result	返回的结果对象
     * @param key		加密key
     * @return
     */
    public static < T > Message< T > successAES( T result , String key)
    {
    	
    	Message<String> Message = new Message<String>(null);
    	
    	Message.setEncryptType(Integer.parseInt(CryptEnum.AES.toString()));
    	
    	AESCrypto aesCrypto = new AESCrypto();
    	
		Message.setResult(aesCrypto.encrypt(JSONObject.toJSONString(result).toString(), key));
		
//		System.out.println(aesCrypto.encrypt(JSONObject.toJSONString(result).toString(), key));
        return (Message<T>) Message;
    }

    public static < T > Message< T > fail( String errorMsg,int errorCode )
    {
        return new Message< T >(errorMsg,errorCode);
    }

    @Override
    public String toString() {
    	return JSONObject.toJSONString(this);
    }
}
